package com.ttdevs.ttrss.ui;

import android.app.ActionBar;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ProgressBar;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.ttdevs.ttrss.R;
import com.ttdevs.ttrss.ui.left.SourceFragment;

public class CustomActivity extends FragmentActivity implements OnShowResultListener {
	private static final int PROGRESSBAR = 0;

	private ActionBar actionBar;
	private SlidingMenu slidingMenu;
	private ProgressBar pbOperate;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case PROGRESSBAR:
				if ((Boolean) msg.obj) {
					pbOperate.setVisibility(View.VISIBLE);
				} else {
					pbOperate.setVisibility(View.GONE);
				}
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_custom);

		initActionBar();
		initSlidingMenu();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onBackPressed() {
		System.out.println("CustomActivity back");
		System.out.println(">>>>>" + slidingMenu.isActivated());
		System.out.println(">>>>>" + slidingMenu.isFocusable());
		System.out.println(">>>>>" + slidingMenu.isMenuShowing());
		if (slidingMenu.isMenuShowing()) {
			slidingMenu.toggle(true);
		} else {
			finish();
		}
	}

	private void initActionBar() {
		actionBar = getActionBar();
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setCustomView(R.layout.custom_actionbar);

		pbOperate = (ProgressBar) actionBar.getCustomView().findViewById(R.id.pbOperate);
	}

	private void initSlidingMenu() {
		slidingMenu = new SlidingMenu(this);// 直接new，而不是getSlidingMenu
		slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		// slidingMenu.setShadowDrawable(R.drawable.shadow);
		slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
		slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		slidingMenu.setBehindWidth(getResources().getInteger(R.integer.sliding_width));// 设置SlidingMenu菜单的宽度
		slidingMenu.setFadeDegree(0.35f);
		slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);// 必须调用

		slidingMenu.setMenu(R.layout.fragment_left);// 就是普通的layout布局
		slidingMenu.setSecondaryMenu(R.layout.fragment_right);

		FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
		fragmentTransaction.replace(R.id.fragment_custom, new CustomFragment());
		// fragmentTransaction.replace(R.id.fragment_left, new LeftFragment());
		fragmentTransaction.replace(R.id.fragment_left, new SourceFragment());
		fragmentTransaction.replace(R.id.fragment_right, new RightFragment());
		fragmentTransaction.commit();
	}

	public SlidingMenu getSlidingMenu() {
		return slidingMenu;
	}

	public void setProgressBar(boolean visible) {
		Message msg = new Message();
		msg.what = PROGRESSBAR;
		msg.obj = visible;
		handler.sendMessage(msg);
	}

	@Override
	public void onShowResult(String url) {
		System.out.println(">>>>>" + url);
		CustomFragment cf = (CustomFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_custom);
		cf.setLink(url);
	}
}
